<?php
class menuModel extends hopeCmsModel
{

	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
    {
        return '{menu}';
    }

    public function primaryKey()
    {
        return 'menu_id';
    }

    /**
     * 获取所有顶级菜单
     */
    public function getTopMenu()
    {
    	return $this->findsByAttribute( 'menu_id, menu_name', 'type=0 AND parent_id=0 AND display=1');
    }

    /**
     * 获取某个顶级菜单下的所有子菜单
     * 
     * @param int $menuId
     */
    public function getChildMenu($menuId)
    {
        $menuTitle          = $this->findsByAttribute( 'menu_id, menu_name, parent_id', 'type=1 AND display=1 AND parent_id='.(int)$menuId);

        foreach ($menuTitle as $k => $menu) {
            $childMenu[$k]['menu_name'] = $menu['menu_name'];
            $childMenu[$k]['parent_id'] = $menu['parent_id'];
            $childMenu[$k]['child'] = $this->findsByAttribute(
                'menu_name, module, control, action, params',
                'type=2 AND display=1 AND parent_id='.$menu['menu_id']
            );
        }

        foreach ($childMenu as $k => $menu) {
            if ($k == 0) {
                $cur = $this->findByPk($menu['parent_id']);
                $menus['url'] = D::createUrl($cur['module'].'/'.$cur['control'].'/'.$cur['action']);
            }
            $menus['name'][$k]  = $menu['menu_name'];
            $menus['child'][$k] = '';
            foreach ($menu['child'] as $k1 => $child) {
                $style = ($k1 === 0) ? 'class="link"' : '';
                if (empty($child['module']) || empty($child['control'])) {
                    $url = '/';
                } else {
                    $url = $child['module'].'/'.$child['control'].'/'.$child['action'];
                }
                $params = empty($child['params']) ? '' : unserialize($child['params']);
                $menus['child'][$k] .= '<li '.$style.' u="'.D::createUrl($url).'" onclick="content_load(\''.D::createUrl($url).'\', \''.D::createUrl($url, $params).'\');">'.$child['menu_name'].'</li>'."\n";
            }
        }
    	return $menus;
    }

}
?>